package com.jerry.usbserial.util.task.usb

import android.content.Context
import com.jerry.usbserial.activity.UsbService
import com.jerry.usbserial.util.*

/**
 * Created by dugang on 2020/6/21.
 */
class UsbMenuTask(context: Context, usbService: UsbService, private val mList: List<MenuItem>) : UsbTask(context, usbService) {
    private val mSendSize = mList.size
    private var mSendIndex = 0
    private var mCrcArray = byteArrayOf()

    override fun start() {
        updateMenuTest()
    }

    private fun updateMenuTest() {
        mStep = STEP_UPDATE_MENU_TEST
        write("AE5AA501000000E11E".hexToByteArray())
    }

    private fun updateMenu() {
        mStep = STEP_UPDATE_MENU
        val menuItem = mList[mSendIndex]

        val cmdList = mutableListOf<Byte>()
        cmdList.addAll("0024A004".hexToByteArray().toList())
        val deviceId = java.lang.Long.toHexString("708901880000".toLong())
        cmdList.addAll(deviceId.hexToByteArray().toList())
        cmdList.addAll("01020302".hexToByteArray().toList())
        cmdList.add(menuItem.id.toByte())
        cmdList.addAll("01".hexToByteArray().toList())
        cmdList.add(menuItem.temputer.toByte())
        cmdList.addAll((menuItem.time * 60).to2HByteArray().toList())
        val name = menuItem.name.toByteArray(charset("GB2312"))
        cmdList.addAll(name.copyInto(20, 32).toList())

        val crcList = mutableListOf<Byte>()
        crcList.addAll("AEE11E".hexToByteArray().toList())
        crcList.addAll(cmdList)
        mCrcArray = CRCUtil.getCrc16(crcList.toByteArray(), crcList.size).to2HByteArray()

        cmdList.addAll(mCrcArray.toList())
        cmdList.addAll("E11E".hexToByteArray().toList())

        val sendList = mutableListOf<Byte>()
        sendList.addAll("AE5AA5".hexToByteArray().toList())
        sendList.addAll(cmdList)
        write(sendList.toByteArray())
    }

    private fun updateMenuFinish() {
        mStep = STEP_UPDATE_MENU_FINISH
        write("AE5AA503000000E11E".hexToByteArray(), false)
        postCompleted("菜单更新完成,设备重启中...")
    }

    override fun onResult(data: ByteArray) {
        super.onResult(data)
        if (data.size != 9 && data.size != 19) return
        when (mStep) {
            STEP_UPDATE_MENU_TEST -> {
                if (data.contentEquals("AE5AA502000000E11E".hexToByteArray())) {
                    updateMenu()
                } else {
                    updateMenuTest()
                }
            }
            STEP_UPDATE_MENU -> {
                val result = ByteArray(2)
                System.arraycopy(data, 15, result, 0, result.size)
                if (result.contentEquals(mCrcArray)) {
                    if (mSendIndex < (mSendSize - 1)) {
                        mSendIndex++
                        postProcess(mSendIndex * 100 / mSendSize)
                        Thread.sleep(200)
                        updateMenu()
                    } else {
                        updateMenuFinish()
                    }
                } else {
                    postError("CRC 校验失败!")
                }
            }
        }
    }

    companion object {
        private const val STEP_UPDATE_MENU_TEST = 6
        private const val STEP_UPDATE_MENU = 7
        private const val STEP_UPDATE_MENU_FINISH = 8
    }
}